main.controller('editorCtrl', ["$scope", "$rootScope", "$location", "$routeParams", "userConfig", "$timeout", "tools", "tmp", "$interval", function ($scope, $rootScope, $location, $routeParams, userConfig, $timeout, tools, tmp, $interval) {
    $scope.toPages = function () {
        if (!tools.mobileEnv()) {
            // 如果距离上一次保存超过二十秒则再提醒一下用户保存
            if ((new Date().getTime() - $scope.latest_save_time) > 10 * 1000 && $scope.userName == $scope.sharerName) {
                tools.confirm({
                    title: '保存页面提醒',
                    content: '在离开前是否先保存?',
                    btns: {
                        yes: {
                            text: '对,先保存',
                            fn: function () {
                                $scope.savePage(function () {
                                    $location.url('/pages');
                                });
                            }
                        },
                        no: {
                            text: '不保存',
                            fn: function () {
                                $location.url('/pages');
                            }
                        },
                        no2: {
                            text: '取消',
                            fn: function () {
                                $rootScope.$broadcast('CONFIRM_OFF_EV');
                            }
                        }
                    }
                });
            } else {
                $location.url('/pages');
            }
        } else {
            $location.url('/mobilePages');
        }
    }

    $timeout(function(){
        $('.editor-container').height(document.documentElement.clientHeight).find('.availableChartsContainer .overflowscroll').height(document.documentElement.clientHeight - 80).mCustomScrollbar({
            theme:'minimal-dark'
        });
    },50)



    $scope.userName = window.localStorage.getItem('userName');
    $scope.sharerName = $routeParams.sharerName;
    $scope.latest_save_time = new Date().getTime();
    $scope.previewing = false;
    $scope.pageName = "";

    // 是否正在预览
    if ($routeParams.type == 'prod') {
        $scope.previewing = true;
        tmp.data.previewing = true;
    } else {
        $scope.previewing = false;
        tmp.data.previewing = false;
    }

    // 检测是否已关注
    var isFocused = function () {
        var userConfigData = userConfig.getUserConfig();
        if (!userConfigData) {
            return false;
        }
        if (!userConfigData.focusedPages || !userConfigData.focusedPages.length) {
            return false;
        }
        for (var i = 0; i < userConfigData.focusedPages.length; i++) {
            if (String(userConfigData.focusedPages[i]) === String($routeParams.id)) {
                return true;
            }
        }
        return false;
    }

    // 获取页面配置
    var page = null;
    tools.get({
        url: '/Ibm-Client/getPageByPageId.htm',
        data: {
            pageId: $routeParams.id
        },
        succ: function (resp) {
            if (resp.success) {
                $scope.pageName = resp.value.pageName;
                // $scope.sharerName = resp.value.sharerName;

                tools.data.acceptPageJson = false;
                if (resp.value.pageObject.jsonObject) {
                    // 先尝试广播一次,如果能接受到话则不需要广播第二次
                    $scope.$broadcast('PAGE_JSON_EV', resp.value.pageObject.jsonObject.core);
                    console.log(resp.value.pageObject.jsonObject.core)

                    // 有时候PAGE_JSON_EV广播出去之后,接受方还未做好接受的准备,导致页面出不来,所以建立一个循环广播直到接受到为止
                    var intv = $interval(function () {
                        if (tools.data.acceptPageJson) {
                            $interval.cancel(intv);
                            return;
                        }

                        if (!tools.data.acceptPageJson) {
                            $scope.$broadcast('PAGE_JSON_EV', resp.value.pageObject.jsonObject.core);
                        } else {
                            $interval.cancel(intv);
                        }
                    }, 500);

                    page = resp.value;
                    // 检测是否已关注
                    $scope.focused = isFocused();
                }
            }
        }
    });

    $scope.exitFullScr = function (e) {
        e.stopPropagation();
        $timeout(function () {
            $rootScope.$broadcast('PREVIEW_OFF_EV');
            $scope.previewing = false;
            tmp.data.previewing = false;
            $timeout(function () {
                $('.chart-manager-container').css({
                    height: document.documentElement.clientHeight - 110 - $(".field-manager-wrap").height()
                }, 400);
            }, 100);
        }, 0);
    }

    // 监听ESC键退出预览
    document.body.onkeydown = function (e) {
        if (e.keyCode == 27) {
            $scope.exitFullScr(e);
        }
    }

    // 菜单栏数据结构
    $scope.menus = [
        {
            name: '文件',
            submenus: [
                {
                    name: '保存',
                    icon: 'save icon',
                    fn: function () {
                        $('.editor-container .ui.dropdown').dropdown('hide');
                        $scope.savePage();
                    }
                },
                {
                    name: '共享',
                    icon: 'share alternate icon',
                    submenus: [
                        // {
                        //     name: '发送邮件',
                        //     icon: 'mail icon'
                        // },
                        {
                            name: '分享给其他账号',
                            icon: 'send outline icon',
                            fn: function () {
                                $scope.sharePage();
                                $('.editor-container .ui.dropdown').dropdown('hide');
                            }
                        }
                    ]
                },
                {
                    name: '新建',
                    icon: 'file icon',
                    fn: function () {
                        tools.confirm({
                            title: '保存页面提醒',
                            content: '在新建页面前是否先保存此页面?',
                            btns: {
                                yes: {
                                    text: '对,先保存',
                                    fn: function () {
                                        $scope.savePage(function () {
                                            $rootScope.$broadcast('NEW_PAGE_EV');
                                        });
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                no: {
                                    text: '不了,直接去创建新的',
                                    fn: function () {
                                        $rootScope.$broadcast('NEW_PAGE_EV');
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                }
                            }
                        });
                    }
                },
                {
                    name: '复制',
                    disabled: true,
                    icon: 'paste icon'
                },
                {
                    name: '重命名',
                    icon: 'write icon',
                    fn: function () {
                        $scope.editTitle();
                        $('.editor-container .ui.dropdown').dropdown('hide');
                    }
                },
                {
                    name: '设置为母版',
                    disabled: true,
                    icon: 'empty star icon'
                },
                {
                    name: '应用标准方格',
                    icon: 'grid layout icon',
                    submenus: [
                        {
                            name: '取消方格',
                            fn: function () {
                                $rootScope.$broadcast('STAND_SQUARE_CANCEL');
                            }
                        },
                        {
                            name: '2行',
                            submenus: [
                                {
                                    name: '2列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 2, 2);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '3列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 2, 3);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '4列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 2, 4);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '5列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 2, 5);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                }
                            ]
                        },
                        {
                            name: '3行',
                            submenus: [
                                {
                                    name: '2列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 3, 2);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '3列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 3, 3);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '4列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 3, 4);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '5列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 3, 5);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                }
                            ]
                        },
                        {
                            name: '4行',
                            submenus: [
                                {
                                    name: '2列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 4, 2);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '3列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 4, 3);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '4列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 4, 4);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '5列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 4, 5);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                }
                            ]
                        },
                        {
                            name: '5行',
                            submenus: [
                                {
                                    name: '2列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 5, 2);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '3列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 5, 3);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '4列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 5, 4);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                },
                                {
                                    name: '5列',
                                    fn: function () {
                                        $rootScope.$broadcast('STAND_SQUARE_EV', 5, 5);
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                    }
                                }
                            ]
                        }
                    ]
                },
                // {
                //     name: '应用母版',
                //     disabled: true,
                //     icon: 'star icon'
                // },
                {
                    name: '下载为',
                    icon: 'download icon',
                    submenus: [
                        {
                            name: 'pdf',
                            icon: 'file pdf outline icon',
                            disabled: true,
                            fn: function () {

                            }
                        },
                        {
                            name: 'word',
                            disabled: true,
                            icon: 'file word outline icon'
                        },
                        {
                            name: 'JPEG图片',
                            disabled: true,
                            icon: 'file image outline icon'
                        },
                        {
                            name: 'png图片',
                            disabled: true,
                            icon: 'file image outline icon'
                        }
                    ]
                },
                {
                    name: '清空所有图表',
                    icon: 'erase icon',
                    fn: function () {
                        $rootScope.$broadcast('CONFIRM_EV', {
                            title: '页面清空',
                            content: '所有图表删除之后不可恢复,确定删除吗?',
                            btns: {
                                yes: {
                                    text: "确定删除",
                                    fn: function () {
                                        $rootScope.$broadcast('DEL_CHART_EV');
                                        $rootScope.$broadcast('DEL_SHEET_EV');
                                        $rootScope.$broadcast('DEL_COND_EV');
                                        $rootScope.$broadcast('DEL_ALL_THINGS');
                                        $('.editor-container .ui.dropdown').dropdown('hide');
                                        // $scope.savePage();
                                    }
                                },
                                no: {
                                    text: '取消',
                                    fn: function () {

                                    }
                                }
                            }
                        });
                    }
                },
                {
                    name: '删除',
                    icon: 'trash outline icon',
                    fn: function () {
                        $('.editor-container .ui.dropdown').dropdown('hide');
                        $rootScope.$broadcast('CONFIRM_EV', {
                            title: '页面操作提醒',
                            content: '删除之后不可恢复,确定删除吗?',
                            btns: {
                                yes: {
                                    text: '确定',
                                    fn: function (e) {
                                        tools.get({
                                            url: '/Ibm-Client/deletePage.htm',
                                            data: {
                                                pageId: $routeParams.id
                                            },
                                            succ: function (resp) {
                                                if (resp.success) {
                                                    $location.url('/pages');
                                                    tools.confirm(false);
                                                }
                                            }
                                        });
                                    }
                                },
                                no: {
                                    text: '取消',
                                    fn: function () {

                                    }
                                }
                            }
                        });
                    }
                }
            ]
        },
        {
            name: '查看',
            submenus: [
                {
                    name: '预览模式',
                    namefn: function () {
                        return 'ddd';
                    },
                    icon: 'laptop icon',
                    fn: function () {
                        $scope.preview();
                        $('.editor-container .ui.dropdown').dropdown('hide');
                    }
                },
                {
                    name: '编辑模式',
                    disabled: true,
                    icon: 'edit icon'
                },
                {
                    name: '移动端模式',
                    disabled: true,
                    icon: 'mobile icon'
                },
                {
                    name: 'PAD模式',
                    disabled: true,
                    icon: 'tablet icon'
                }
            ]
        },
        {
            name: '编辑',
            submenus: [
                {
                    name: '撤销',
                    disabled: true
                },
                {
                    name: '全选(ctrl+A)',
                    disabled: true
                },
                {
                    name: '取消全选',
                    disabled: true
                }
            ]
        },
        // {
        //     name: '格式',
        //     submenus: [
        //         {
        //             name: '字体',
        //             icon: 'font icon'
        //         },
        //         {
        //             name: '加粗',
        //             icon: 'bold icon'
        //         },
        //         {
        //             name: '斜线',
        //             icon: 'italic icon'
        //         },
        //         {
        //             name: '下划线',
        //             icon: 'underline icon'
        //         },
        //         {
        //             name: '左对齐',
        //             icon: 'align left icon'
        //         },
        //         {
        //             name: '居中对齐',
        //             icon: 'align justify icon'
        //         },
        //         {
        //             name: '右对齐',
        //             icon: 'align right icon'
        //         },
        //         {
        //             name: '行高',
        //             icon: 'text height icon'
        //         }
        //     ]
        // },
        {
            name: '插入',
            submenus: [
                {
                    name: '图',
                    icon: 'pie chart icon',
                    submenus: [
                        {
                            name: '饼图',
                            fn: function () {
                                $scope.addChart('pie');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '嵌套饼图',
                            fn: function () {
                                $scope.addChart('nestedPie');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '折线图',
                            fn: function () {
                                $scope.addChart('line');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '柱状图',
                            fn: function () {
                                $scope.addChart('bar');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '条形图',
                            fn: function () {
                                $scope.addChart('verBar');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '瀑布图',
                            fn: function () {
                                $scope.addChart('waterfall');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '区域图',
                            fn: function () {
                                $scope.addChart('area');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '气泡图',
                            fn: function () {
                                $scope.addChart('bubble');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '散点图',
                            fn: function () {
                                $scope.addChart('scatter');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '雷达图',
                            fn: function () {
                                $scope.addChart('radar');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '漏斗图',
                            fn: function () {
                                $scope.addChart('funnel');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '直方图',
                            fn: function () {
                                $scope.addChart('histogram');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '面积图',
                            fn: function () {
                                $scope.addChart('treemap');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '仪表盘',
                            fn: function () {
                                $scope.addChart('dashboard');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '省份地图',
                            fn: function () {
                                $scope.addChart('provinceMap');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '城市地图',
                            fn: function () {
                                $scope.addChart('cityMap');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        }
                    ]
                },
                {
                    name: '表',
                    icon: 'table icon',
                    submenus: [
                        {
                            name: '汇总表',
                            fn: function () {
                                $scope.addSheet('summarytab');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '交叉表',
                            fn: function () {
                                $scope.addSheet('crosstab');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        },
                        {
                            name: '明细表',
                            fn: function () {
                                $scope.addSheet('detailtab');
                                $('.editor-header-funs-menus .ui.dropdown').dropdown({
                                    action: 'hide'
                                });
                            }
                        }
                    ]
                },
                {
                    name: '图片',
                    disabled: true,
                    icon: 'file image outline icon',
                    fn: function () {

                    }
                },
                {
                    name: '背景',
                    disabled: true,
                    icon: 'file image outline icon',
                    fn: function () {
                    }
                },
                {
                    name: '文本框',
                    icon: 'object ungroup icon',
                    fn: function () {
                        $rootScope.$broadcast('ADD_TEXT_EV', 'text');
                        $('.editor-container .ui.dropdown').dropdown('hide');
                    }
                }
            ]
        },
        // {
        //     name: '图表',
        //     disabled: true,
        //     submenus: [
        //         {
        //             name: '导入excel数据',
        //             disabled: true
        //         },
        //         {
        //             name: '下载',
        //             disabled: true
        //         },
        //         {
        //             name: '转化为图'
        //         },
        //         {
        //             name: '转化为表'
        //         },
        //         {
        //             name: '清除数据',
        //             fn: function () {
        //                 $rootScope.$broadcast('CLEAR_DATA_EV');
        //             }
        //         }
        //     ]
        // },
        {
            name: '帮助',
            submenus: [
                {
                    name: '反馈问题',
                    fn: function () {
                        $rootScope.$broadcast('FEED_BACK_EV');
                    }
                }/*,
                 {
                 name: '关于趋数'
                 },
                 {
                 name: '软件续费'
                 }*/
            ]
        }
    ];

    $timeout(function () {
        $('.editor-header-funs-menus .ui.dropdown').dropdown({
            action: 'nothing'
        }).click(function (e) {
            $(e.target).closest('.dropdown').find('.menu').css('zIndex', dropdown_z_index++);
        });
    }, 1000);

    $scope.addChart = function (type) {
        $rootScope.$broadcast('ADD_CHART_EV', type);
    }

    $scope.addSheet = function (type) {
        $rootScope.$broadcast('ADD_SHEET_EV', type);
    }

    $scope.addCondition = function (type) {
        $rootScope.$broadcast('ADD_CONDITION_EV', type);
    }

    $scope.addObject = function (type) {
        $rootScope.$broadcast('ADD_TEXT_EV', type);
    }

    $scope.addMap = function (type) {
        $rootScope.$broadcast('ADD_MAP_EV', type);
    }

    $scope.savePage = function (cb) {
        tools.msg('请稍等,正在保存...');
        $rootScope.$broadcast('GET_PAGE_JSON_EV', function (pageJson) {
            $timeout(function () {
                $.post('/Ibm-Client/updatePage.htm', {
                    'chartStyle': 'default',
                    'tbId': $routeParams.tbId,
                    'pageId': $routeParams.id,
                    'pageName': $scope.pageName,
                    'tableType': $routeParams.tableType,
                    'pageJson': "{'jsonObject':" + pageJson + '}'
                }, function (resp) {
                    resp = JSON.parse(resp);
                    if (resp.success) {
                        tools.msg('保存成功!');
                    } else {
                        tools.msg('系统繁忙,请稍后再试!');
                    }
                    $scope.latest_save_time = new Date().getTime();
                    cb && cb();
                });
            }, 1500);
        });
    }

    $scope.sharePage = function (e) {
        $rootScope.$broadcast('SHARE_EV', $routeParams.id, $scope.pageName);
    }

    $scope.editTitle = function () {
        $rootScope.$broadcast('INPUT_EDIT_EV', $scope.pageName, function (content) {
            $scope.pageName = content;
        });
    }

    $scope.focusAttention = function () {
        var userConfigData = userConfig.getUserConfig();
        if (!userConfigData.focusedPages) {
            userConfigData.focusedPages = [];
        }

        if (!$scope.focused) {
            userConfigData.focusedPages.push($routeParams.id);
            userConfig.updateUserConfig();
            $scope.focused = true;
        } else {
            for (var i = 0; i < userConfigData.focusedPages.length; i++) {
                if (String(userConfigData.focusedPages[i]) == String($routeParams.id)) {
                    userConfigData.focusedPages.splice(i, 1);
                    break;
                }
            }
            userConfig.updateUserConfig();
            $scope.focused = false;
        }
    }

    $scope.isFocused = function () {

    }

    $scope.chartInfo = "";
    $scope.sheetInfo = "";
    $scope.condInfo = "";

    $scope.getChartInfo = function (chartInfo) {
        $scope.chartInfo = chartInfo;
    }
    $scope.getSheetInfo = function (sheetInfo) {
        $scope.sheetInfo = sheetInfo;
    }
    $scope.getCondInfo = function (condInfo) {
        $scope.condInfo = condInfo;
    }

    $scope.preview = function () {
        $scope.previewing = true;
        $('.ui.dropdown').dropdown('hide');
        $rootScope.$broadcast('PREVIEW_EV');
        $('.chart-manager-container').height(document.documentElement.clientHeight - $('.editor-header').height() - 10);
        $timeout(function () {
            $rootScope.$broadcast('MESSAGE_EV', '按ESC键退出全屏');
        }, 600);
    }

    $scope.changeFontStyle = function (key, value) {
        $rootScope.$broadcast('FONT_STYLE_CHANGE_EV', key, value);
    }

    $rootScope.$on('SAVE_PAGE_EV', function () {
        $scope.savePage();
    });

    $scope.$on('destroy', function () {
        tools.data.acceptPageJson = false;
    });
}]);
/**
 * Created by xuchaosheng on 16/5/22.
 */
